package com.kitchen.ui.dialog.decorate
{
	import com.kitchen.KitchenEvent;
	import com.kitchen.http.KitchenProxy;
	import com.kitchen.http.Proxy;
	
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	
	internal class DecoGrid extends Sprite
	{
		private var dialogSkin:BitmapData;
		private var timer:Timer;
		private var proxy:KitchenProxy = null;
		private var cellPage:Array;
		public var page:int;
		private var nPage:int;
		public var pt:int;
		private var nPt:int;
		public var ptotal:int;
		private var container:Sprite;
		private var tox:int;
		
		public function DecoGrid(dialogSkin:BitmapData)
		{
			super();
			this.dialogSkin = dialogSkin;
			this.cellPage = new Array();
			this.timer = new Timer(50);
			this.page = 0;
			this.nPage = 0;
			this.nPt = 0;
			this.pt = 1;
			this.ptotal = 2;
			this.timer.addEventListener(TimerEvent.TIMER, timerHandler);
			this.container = new Sprite();
			this.addChild(this.container);
			var mshape:Shape =new Shape();
			mshape.graphics.beginFill(0xffffff, 1);
			mshape.graphics.drawRect(0, 0, 420, 96);
			this.mask = mshape;
			this.addChild(mask);
		}
		
		public function goPage(page:int, st:int):void{
			if(this.nPage > 0){
				return;
			}
			this.nPage = page;
			this.nPt = st;
			this.getProxy().getSysDecos(this.nPt, this.nPage);
		}
		
		public function nextPage(st:int):void{
			if(this.page < this.ptotal){
				this.goPage(this.page+1, st);
			}
		}
		
		public function prevPage(st:int):void{
			if(this.page > 1){
				this.goPage(this.page-1, st);
			}
		}
		
		private function getProxy():KitchenProxy{
			if(this.proxy == null){
				this.proxy = KitchenProxy.getInstance();
				this.proxy.addEventListener(Proxy.E_OK, resultHander);
				this.proxy.addEventListener(Proxy.E_CERROR, errorHandler);
			}
			return this.proxy;
		}
		
		private function show(result:Object):void{
			this.ptotal = result.pager.ptotal;
			var key:int = int(this.nPt+''+this.nPage);
			if(this.cellPage[key] == null){
				var cdata:Array = result.data;
				var offx:int = (this.nPage-1)*440;
				var i:int = 0;
				var cells:Array = new Array();
				for each(var drow:Object in cdata){
					var cell:DecoGridCell = new DecoGridCell(this.dialogSkin, drow);
					cell.x = i*88+offx;
					cells.push(cell);
					this.container.addChild(cell);
					i++;
				}
				this.cellPage[key] = cells;
			}
			this.tox = -(this.nPage-1)*440;
			this.timer.start();
		}
		
		private function resultHander(event:Event):void{
			var result:Object = this.proxy.result;
			if(result.code == 200){
				this.show(result.result);
			}else{
				trace(result.code+':'+result.message);
			}
		}
		
		private function timerHandler(event:TimerEvent):void{
			if(this.container.x != this.tox){
				if(Math.abs(this.container.x - this.tox) > 10){
					this.container.x = Math.round((this.container.x + this.tox)/2);
				}else{
					this.container.x = this.tox;
				}
			}else{
				var key:int = int(this.pt+''+this.page);
				if(this.cellPage[key] != null){
					for each (var cell:DecoGridCell in this.cellPage[key]){
						this.container.removeChild(cell);
					}
					this.cellPage[key] = null;
				}
				this.page = this.nPage;
				this.pt = this.nPt;
				this.nPage = 0;
				this.nPt = 0;
				this.timer.stop();
			}
		}
		
		private function errorHandler(event:Event):void{
			trace('DecoGrid.errorHandler:'+event.type);
		}
	}
}